package controllers;

import beans.WebServiceBean;
import models.Album;
import models.Collection;
import models.Photo;
import models.Trend;
import play.libs.Codec;
import play.libs.Files;
import play.mvc.Controller;
import play.mvc.With;
import utils.Config;
import utils.SessionUtils;

import java.io.File;

/**
 * Created by xieyicheng on 2015/2/7.
 */
@With(value = CheckLogin.class)
public class Albums extends Controller {
    public static void addAlbum(Album album,String albumId, File image)
    {
        String userId = SessionUtils.getUserId();
        if(userId == null)
        {
            Application.login();
        }
        if(image==null||album.albumName==null||album.info==null){
            Members.addAlbum("请填写完整信息");
        }
        else{
            String imgName=image.getName();
            String unique = Codec.UUID().replace("-", "").toUpperCase();
            File storeFile=new File(Config.imagePath+unique+imgName);
            Files.copy(image, storeFile);
            Photo photo = Photo.addPhoto(unique+imgName,null);
            album.photoId = photo.id;
            album.authorId = userId;
            album.save();
            Trend.addTrend(userId, album.id, Trend.ALBUM);//增加动态
            WebServiceBean webServiceBean = WebServiceBean.getSuccessInstance();
            webServiceBean.data = "新建成功！";
            Members.album(0);
        }

    }
    /**
     *@author 李宇星
     *删除专辑
     **/
    public static void deleteAlbum(String albumId)
    {
        String userId = SessionUtils.getUserId();
        Album album = Album.findById(albumId);
        Trend.deleteTrend(album.id, Trend.ALBUM);//先删除动态
        Photo.deletePhoto(album.photoId);//删除封面
        Collection.deleteRelatedCollection(album.id, Collection.ALBUM);//删除相关收藏
        album.delete();
        Members.album(0);

    }
}
